package com.avengers.core.kit;

import com.avengers.core.bean.enums.HttpAgent;
import com.avengers.core.bean.enums.HttpEnum;
import com.avengers.core.bean.enums.SymbolEnum;
import com.jfinal.kit.StrKit;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author yu.wang
 * @since 2019/3/13 10:22 AM
 **/
public class ControllerKit {
    public static String[] getParaValues(HttpServletRequest request, String name) {
        String[] values = request.getParameterValues(name);

        if (null == values || values.length == 0) {
            name += SymbolEnum.brackets.string;
            values = request.getParameterValues(name);
        }

        return values;
    }

    public Integer[] getParaValuesToInt(HttpServletRequest request, String name) {
        String[] values = getParaValues(request, name);
        if (values != null && values.length != 0) {
            Integer[] result = new Integer[values.length];
            for (int i = 0; i < result.length; ++i) {
                result[i] = StrKit.isBlank(values[i]) ? null : Integer.parseInt(values[i]);
            }
            return result;
        } else {
            return null;
        }
    }

    /**
     * 获取来源actionKey
     */
    public static String getRefererActionKey(HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        String actionKey = request.getServletPath();
        String host = url.substring(0, url.indexOf(actionKey));

        String referer = request.getHeader(HttpEnum.REFERER.value);
        if (StrKit.isBlank(referer)) {
            return null;
        }
        String requestUrl = referer.replace(host, SymbolEnum.empty.string);
        return requestUrl.contains(SymbolEnum.question.string) ? requestUrl.substring(0, requestUrl.indexOf(SymbolEnum.question.string)) : requestUrl;
    }

    /**
     * 获取客户端ip
     */
    public static String getClientIp(HttpServletRequest request) {
        String clientIp = request.getHeader(HttpEnum.X_REAL_IP.value);
        if (StrKit.isBlank(clientIp)) {
            clientIp = request.getHeader(HttpEnum.PROXY_CLIENT_IP.value);
        }
        if (StrKit.isBlank(clientIp) || HttpEnum.UNKNOWN.value.equalsIgnoreCase(clientIp)) {
            clientIp = request.getHeader(HttpEnum.X_FORWARDED_FOR.value);
        }
        if (StrKit.isBlank(clientIp) || HttpEnum.UNKNOWN.value.equalsIgnoreCase(clientIp)) {
            clientIp = request.getHeader(HttpEnum.WL_PROXY_CLIENT_IP.value);
        }
        if (StrKit.isBlank(clientIp) || HttpEnum.UNKNOWN.value.equalsIgnoreCase(clientIp)) {
            clientIp = request.getRemoteAddr();
        }
        if (HttpEnum.LOCALHOST_IP.value.equals(clientIp) || HttpEnum.LOCALHOST.value.equals(clientIp)) {
            try {
                clientIp = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException ignore) {
            }
        }

        return clientIp;
    }

    /**
     * 是否为异步请求
     */
    public static boolean isAjax(HttpServletRequest request) {
        String actionKey = request.getServletPath();
        boolean ajax = HttpEnum.XML_HTTP_REQUEST.value.equals(request.getHeader(HttpEnum.X_REQUESTED_WITH.value));
        boolean ajaxFlag = actionKey.contains(HttpEnum.AJAX.value);

        return ajax || ajaxFlag;
    }

    /**
     * 是否为移动端请求
     */
    public static boolean isMobile(HttpServletRequest request) {
        String agent = request.getHeader(HttpEnum.USER_AGENT.value);
        String[] deviceArray = new String[]{HttpAgent.android.value, HttpAgent.iphone.value, HttpAgent.windowsPhone.value};
        if (null == agent || agent.trim().isEmpty()) {
            return false;
        }
        agent = agent.toLowerCase();
        for (String aDeviceArray : deviceArray) {
            if (agent.indexOf(aDeviceArray) > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否为微信端请求
     */
    public static boolean isWeChat(HttpServletRequest request) {
        String agent = request.getHeader(HttpEnum.USER_AGENT.value).toLowerCase();
        return agent.contains(HttpAgent.weChat.value);
    }
}
